package com.beeantec.jnrerc.modules.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.core.api.dto.PagingReqDTO;
import com.beeantec.jnrerc.dto.MemberVo;
import com.beeantec.jnrerc.dto.request.MemberExamReqDTO;
import com.beeantec.jnrerc.dto.response.MemberExamRespDTO;

import com.beeantec.jnrerc.entity.MemberExam;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import com.beeantec.jnrerc.modules.user.service.MemberExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* <p>
* 考试记录控制器
* </p>
*
* @author Anthony
* @since 2020-09-21 15:13
*/
@Api(tags={"考试记录"})
@RestController
@RequestMapping("/api/memberexam")
public class MemberExamController extends BaseController {

    @Autowired
    private MemberExamService baseService;

    @Autowired
    private MemberService memberService;
    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<MemberExamRespDTO>> paging(@RequestBody PagingReqDTO<MemberExamReqDTO> reqDTO) {

        //分页查询并转换
        IPage<MemberExamRespDTO> page = baseService.paging(reqDTO);

        return super.success(page);
    }


    /**
     * 获取用户考试详情
     * @param
     * @return
     */
    @ApiOperation(value = "获取用户考试详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.GET})
    public ApiRest<MemberExam> paging(@RequestHeader("token") String token ,@RequestParam("examId") String examId) {
        MemberVo memberVo = memberService.token(token);
        if (null==memberVo){
            return failure("用户不存在");
        }
        QueryWrapper<MemberExam> examQueryWrapper = new QueryWrapper<>();
        examQueryWrapper.lambda().eq(MemberExam::getUserId,memberVo.getMemberId().toString())
                .eq(MemberExam::getExamId,examId);
        MemberExam memberExam = baseService.getOne(examQueryWrapper);

        return super.success(memberExam);
    }



    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/my-paging", method = { RequestMethod.POST})
    public ApiRest<IPage<MemberExamRespDTO>> MyPaging(@RequestHeader("token")String token,@RequestBody PagingReqDTO<MemberExamReqDTO> reqDTO) {
        MemberVo memberVo = memberService.token(token);
        reqDTO.setUserId(memberVo.getMemberId().toString());
        //分页查询并转换
        IPage<MemberExamRespDTO> page = baseService.myPaging(reqDTO);

        return super.success(page);
    }
}
